<?
class Area {

	protected	$data			= array();
	protected	$lista_actores;
	protected	$lista_objs;

	function __construct ($nome, $descricao, $terreno) {
		$this->data['nome']			= $nome;
		$this->data['descricao']	= $descricao;
		$this->data['iluminacao'] 	= 1;
		$this->data['terreno'] 		= $terreno;
		
		$this->lista_actores	= new ListaDeElementos();
		$this->lista_objs		= new ListaDeElementos();
	}
	
	public function getValue($key) {
		return $this->data[$key];
	}
	
	public function setCoords($xCoord, $yCoord) {
		$this->data['x'] = $xCoord;
		$this->data['y'] = $yCoord;
	}
	
	public function getCoords() {
		$array['x'] = $this->data['x'];
		$array['y'] = $this->data['y'];
		return $array;
	}
	
	public function getActorById($id) {
		return $this->lista_actores->devolvePorId($id);
	}
	
	public function getObjById($id) {
		return $this->lista_objs->devolvePorId($id);
	}
	
	public function getObjByAtrib($atrib, $valor) {
		return $this->lista_objs->devolvePorAtrib($atrib, $valor);
	}
	
	public function existeObj($obj) {
		return $this->lista_objs->existe($obj);
	}
	
	public function adicionaObj($obj) {
		if (!$this->lista_objs->existe($obj)) {
			return $this->lista_objs->adiciona($obj);
		}
	}
	
	public function removeObj($obj) {
		return $this->lista_objs->remove($obj);
	}
	
	public function getActorByName($nome, $manager) {
		if ($this->lista_actores->existe($nome)) return $tpers = $manager->readObj($nome, 'actores');
		else return false;
	}
	
	public function existeActor($actor) {
		return $this->lista_actores->existe($actor->getValue('nome'));
	}
	
	public function adicionaActor($actor) {
		if (!$this->lista_actores->existe($actor->getValue('nome'))) {
			if ($this->lista_actores->adiciona($actor->getValue('nome'))) {
				$actor->setArea($this);
				return true;
			} else return false;
		}
	}
	
	public function removeActor($actor) {
		return $this->lista_actores->remove($actor->getValue('nome'));
	}
	
	public function listaActoresHtml($html, $excepto, $manager) {
		$thtml = '';
		for ($x=0; $x<$this->lista_actores->numElementos(); $x++) {
			$tpers_nome = $this->lista_actores->devolve($x);
			$tpers = $manager->readObj($tpers_nome, 'actores');
			if ($tpers->getValue('nome') != $excepto->getValue('nome')) $thtml .= $tpers->toResumeHtml($html);
		}
		return $thtml;
	}
	
	public function listaObjsHtml($html) {
		$thtml = '';
		for ($x=0; $x<$this->lista_objs->numElementos(); $x++) {
			$tobj = $this->lista_objs->devolve($x);
			$thtml .= $tobj->getAreaHtmlLink($html);
		}
		return $thtml;
	}
	
	public function areaObjsListHtml($html) {
		$thtml = ''; $thtmllink = '';
		for ($x=0; $x<$this->lista_objs->numElementos(); $x++) {
			$tobj = $this->lista_objs->devolve($x);
			$thtml .= $tobj->toHtmlImg($html);
			$thtmllink .= $tobj->toHtmlImgLink($html);
		}
		$this->data['areaObjsListHtml'] = $thtml;
		$this->data['areaObjsListHtmlLink'] = $thtmllink;
	}
	
	public function toHtml($dir, $html) {
		$move_dir['move_dir'] = $dir;
		if ($dir == 'actual') $div_template = 'area_actual_div'; else $div_template = 'area_move_div';
		$html->set($div_template, $html->mixArray($this->getArea($this->getCoords($dir))->toArray(), $div_template));
		$html->set($div_template, $html->mixArray($this->getArea($this->getCoords($dir))->areaObjsListHtml($html), $div_template));
		return $html->get($div_template);
	}
	
	public function toArray() {
		return $this->data;
	}
}
?>